// call实现
Function.prototype.myCall = function (context) {
  context = context || window;
  context.fn = this;
  const args = [];
  // 收集参数
  for (let i = 1; i < arguments.length; i++) {
    args.push(`arguments[${i}]`);
  }

  const result = eval(`context.fn(${args})`);
  delete context.fn;
  return result;
};

// apply
Function.prototype.myApply = function (context, args) {
  context = context || window;
  context.fn = this;
  args = args || [];
  const _argsStr = args.reduce((curr, next, index) => [...curr, `args[${index}]`], []);
  console.log(_argsStr);
  const result = eval(`context.fn(${_argsStr})`);

  delete context.fn;
  return result;
};

const fun = function (name) {
  console.log(name);
  return this.name + 'test';
};
// console.log(fun.myCall({ name: 11 }, 'name'));

console.log(fun.myApply({ name: 11 }, ['name111']));
